// js 错误 文件信息，错误类型，调用栈，
/**
 * {
  "title": "前端监控系统",//页面标题
  "url": "http://localhost:8080/",//页面URL
  "timestamp": "1590815288710",//访问时间戳
  "userAgent": "Chrome",//用户浏览器类型
  "kind": "stability",//大类
  "type": "error",//小类
  "errorType": "jsError",//错误类型
  "message": "Uncaught TypeError: Cannot set property 'error' of undefined",//类型详情
  "filename": "http://localhost:8080/",//访问的文件名
  "position": "0:0",//行列信息
  "stack": "btnClick (http://localhost:8080/:20:39)^HTMLInputElement.onclick (http://localhost:8080/:14:72)",//堆栈信息
  "selector": "HTML BODY #container .content INPUT"//选择器
}
 */
import getLastEvent from '../utils/getLastEvent'
import getSelect from '../utils/getSelector'
export function injectJsError() {
	// const getLastEvent = () => {}
	window.addEventListener('error', function (event) {
		console.log('%c [  ]-20', 'font-size:13px; background:pink; color:#bf2c9f;', event)

		let lastEvent = getLastEvent()
		console.log('%c [  ]-23', 'font-size:13px; background:pink; color:#bf2c9f;', lastEvent.target.tagName)
		let log = {
			kind: 'stability', // 监控指标 大类
			type: 'error',
			errorType: 'jsError',
			message: event.message,
			filename: event.filename,
			position: `${event.lineno}:${event.colno}`,
			stack: getLine(event.error.stack),
			selection: lastEvent ? getLastEvent() : '' //代表最后操作的一个元素
			// stack: event.error.stack
		}
		console.log('log', log)
		function getLine(stack) {
			return stack
				.split('\n')
				.slice(1)
				.map((item) => item.replace(/^\s+at\s+/g), '^')
		}
		function getLast() {}
		// .join('^')

		console.log('%c [  ]-28', 'font-size:13px; background:pink; color:#bf2c9f;', log)
		// console.log(event.error)
		// console.log(event.filename)
		// console.log(event.lineno)
		// console.log(event.colno)
		// console.log(event.message)
		// console.log(event.error.stack)
	})
}
